package jun.work.io;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class StreamUtils {
	public static String getStringByIs(InputStream is,String code){
		if(is == null){
			throw new IllegalArgumentException("输入流为空");
		}
		ByteArrayOutputStream bas = new ByteArrayOutputStream();
		byte[] buf = new byte[1024];
		int len = 0;
		try {
			while((len = is.read(buf)) != -1){
				bas.write(buf, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			return new String(bas.toByteArray(), code);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
}
